package locationData;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.lcdui.ChoiceGroup;
public class LocationParse {
    
      Vector countries,timeZone;
      Vector towns, cities, latitude, longitude, tLatitude, tLongitude; 
   
   
public  ChoiceGroup modifyCountryChoiceGroup(String fileName, ChoiceGroup countryGrp) 
    {    try {
            
            InputStream inputStreamTxt = null;

            inputStreamTxt = this.getClass().getResourceAsStream("/country/"+fileName);
            StringBuffer buf = new StringBuffer();
            countries = new Vector();
            timeZone = new Vector();
            int c;
            while ((c = inputStreamTxt.read()) != -1) {
                char ch = (char) c;
                if (ch == '\n')
                {
                    timeZone.addElement(buf.toString());
                    buf.delete(0, buf.length());
                }
                else if (ch == ',') {
                    countries.addElement(buf.toString());
                    buf.delete(0, buf.length());
                } else {
                    buf.append(ch);
                }
            }

            inputStreamTxt.close();
        
        } catch (IOException ex) {
            ex.printStackTrace();
        }
           for(int i=0; i<countries.size(); i++)
           {

            countryGrp.append(countries.elementAt(i).toString(),null);   
           }
           return countryGrp;
}
 
 
 public  ChoiceGroup modifyCityChoiceGroup(String fileName, ChoiceGroup cityGrp) 
    {  
     boolean isCityDone=false;
     //cityGrp=null;
    try {
            InputStream inputStreamTxt = null;
            inputStreamTxt = this.getClass().getResourceAsStream("/country/"+fileName+".txt");
     
            StringBuffer buf = new StringBuffer();
            cities = new Vector();
            latitude = new Vector();
            longitude = new Vector();
            int c;
            
            while ((c = inputStreamTxt.read()) != -1) {
                char ch = (char) c;
                if (ch == '\n')
                {
                    longitude.addElement(buf.toString());
                    buf.delete(0, buf.length());
                }
                else if ((ch == ',')&&!isCityDone) {
                    cities.addElement(buf.toString());
                    buf.delete(0, buf.length());
                    isCityDone=true;
                }  else if ((ch == ',')&&isCityDone) {
                    latitude.addElement(buf.toString());
                    buf.delete(0, buf.length());
                    isCityDone=false;
                } else {
                    buf.append(ch);
                }
            }

            inputStreamTxt.close();
           
       // System.out.println( cities);
      //  System.out.println( latitude);
       // System.out.println( cities.indexOf("Ankara"));
       // System.out.println(latitude.elementAt(cities.indexOf("Ankara")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
           // cityGrp = new ChoiceGroup(LocalizationSupport.getMessage("PROVINCEGRP"), Choice.POPUP);
     cityGrp.deleteAll();       
     for(int i=0; i<cities.size(); i++)
     {
         cityGrp.append(cities.elementAt(i).toString(),null);   
     }
     return cityGrp;
}
 
 public  ChoiceGroup modifyTownChoiceGroup(String fileName, String cityFileName, ChoiceGroup townGrp) 
    {
   
     boolean isTownDone=false;
     String  currentLat,currentLong = null;
     
     townGrp.deleteAll();
     towns = new Vector();
     currentLat=(String) latitude.elementAt(cities.indexOf(cityFileName));
     currentLong=(String) longitude.elementAt(cities.indexOf(cityFileName));
     latitude = new Vector();
     longitude = new Vector();
     towns.addElement(cityFileName);
    //    System.out.println(towns);
     latitude.addElement(currentLat);
     longitude.addElement(currentLong);
     townGrp.append(towns.elementAt(0).toString(),null); 
    //    System.out.println( latitude);
    //    System.out.println( longitude);
    try {
            InputStream inputStreamTxt = null;
            inputStreamTxt = this.getClass().getResourceAsStream("/country/"+fileName+"/"+cityFileName+".txt");
            StringBuffer buf = new StringBuffer();
            int c;
            if (inputStreamTxt==null) return townGrp;
            while ((c = inputStreamTxt.read()) != -1) {
                char ch = (char) c;
                if (ch == '\n')
                {
                    longitude.addElement(buf.toString());
                    buf.delete(0, buf.length());
                }
                else if ((ch == ',')&&!isTownDone) {
                    towns.addElement(buf.toString());
                    buf.delete(0, buf.length());
                    isTownDone=true;
                }  else if ((ch == ',')&&isTownDone) {
                    latitude.addElement(buf.toString());
                    buf.delete(0, buf.length());
                    isTownDone=false;
                } else {
                    buf.append(ch);
                }
            }

            inputStreamTxt.close();
            
           
       // System.out.println(towns);
       // System.out.println( latitude);
        //System.out.println( longitude);
       // System.out.println( cities.indexOf("Ankara"));
       // System.out.println(latitude.elementAt(cities.indexOf("Ankara")));
        } catch (IOException ex) {
            ex.printStackTrace();
        
                  }
           // cityGrp = new ChoiceGroup(LocalizationSupport.getMessage("PROVINCEGRP"), Choice.POPUP);
        townGrp.deleteAll();
      /*  System.out.println(towns);
        System.out.println( latitude);
        System.out.println( longitude);
        System.out.println(towns.size());*/
     //townGrp.append(cityFileName,null); 
     for(int i=0; i<towns.size(); i++)
     {
         townGrp.append(towns.elementAt(i).toString(),null);   
     }
     return townGrp;
} 
public float getTimeZoneValue (int index)
 {
    return Float.parseFloat(timeZone.elementAt(index).toString());
 }
 public String getSelectedCountry (int index)
 {
    return countries.elementAt(index).toString();
 }
 
public String getSelectedCity (int index)
 {
    return cities.elementAt(index).toString();
 }
public String getSelectedTown(int index)
 {
    return towns.elementAt(index).toString();
 }
public float getSelectedLatitude (int index)
 {
    return Float.parseFloat(latitude.elementAt(index).toString());
 }
public float getSelectedLongitude (int index)
 {
    return Float.parseFloat(longitude.elementAt(index).toString());
 }

  
}
